package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.domain.CldProjectPost;
import com.cleaningcloud.cld.domain.CldStaffContract;
import com.cleaningcloud.cld.domain.dto.CldProjectPostDto;
import com.cleaningcloud.cld.domain.dto.CldStaffContractDto;
import com.cleaningcloud.cld.domain.vo.CldProjectPostSetVo;
import com.cleaningcloud.cld.domain.vo.CldProjectSetVo;
import com.cleaningcloud.cld.domain.vo.CldStaffContractVo;
import com.cleaningcloud.cld.mapper.CldProjectPostMapper;
import com.cleaningcloud.cld.service.ICldProjectPostService;
import com.cleaningcloud.cld.service.ICldStaffContractService;
import com.cleaningcloud.cld.service.ICldStaffService;
import com.cleaningcloud.cld.service.PublicCommonService;
import com.cleaningcloud.common.core.constant.SecurityConstants;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.DictUtils;
import com.cleaningcloud.erp.api.RemoteWareService;
import com.cleaningcloud.erp.api.domain.ErpWareHouse;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 项目岗位Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-15
 */
@Service
public class CldProjectPostServiceImpl implements ICldProjectPostService {
    @Autowired
    private CldProjectPostMapper cldProjectPostMapper;

    @Autowired
    private PublicCommonService publicCommonService;

    @Autowired
    private ICldStaffContractService staffContractService;

    @Autowired
    private RemoteWareService remoteWareService;

    @Autowired
    private ICldStaffService cldStaffService;


    /**
     * 查询项目岗位
     *
     * @param projectPostId 项目岗位主键
     * @return 项目岗位
     */
    @Override
    public CldProjectPostSetVo selectCldProjectPostByProjectPostId(Long projectPostId) {
        return cldProjectPostMapper.selectCldProjectPostByProjectPostId(projectPostId);
    }

    /**
     * 查询项目岗位列表
     *
     * @param cldProjectPost 项目岗位
     * @return 项目岗位
     */
    @Override
    public List<CldProjectPostSetVo> selectCldProjectPostList(CldProjectPostDto cldProjectPost) {
        return cldProjectPostMapper.selectCldProjectPostList(cldProjectPost);
    }

    @Override
    public List<CldProjectPostSetVo> getProjectPostList(CldProjectPostDto cldProjectPostDto) {
        return cldProjectPostMapper.getProjectPostList(cldProjectPostDto);
    }

    /**
     * 新增项目岗位
     *
     * @param cldProjectPost 项目岗位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldProjectPost(CldProjectSetVo cldProjectPost) {
        return 1;
    }


    private void updateStaffContract(CldProjectPost projectPost, CldProjectPost cldProjectPost) {
        // 查询之前是否有人员id
        if (projectPost.getStaffId() == null) {
            if (cldProjectPost != null) {
                publicCommonService.staffOptions(cldProjectPost.getStaffId(), 3, cldProjectPost.getProjectId(), cldProjectPost.getProjectPostId());
            }
        } else {
            if (!Objects.equals(cldProjectPost.getStaffId(), projectPost.getStaffId())) {
                // 查询该人员是否签订人事合同
                CldStaffContractDto staffContractQuery = new CldStaffContractDto();
                staffContractQuery.setStaffId(projectPost.getStaffId());
                staffContractQuery.setProjectId(projectPost.getProjectId());
                staffContractQuery.setContractStatus(1L);
                List<CldStaffContractVo> staffContractList = staffContractService.selectCldStaffContractList(staffContractQuery);
                if (staffContractList.isEmpty()) {
                    CldStaffContract staffContract = new CldStaffContract();
                    staffContract.setStaffId(projectPost.getStaffId());
                    staffContract.setJobId(projectPost.getProjectPostId());
                    staffContract.setProjectId(projectPost.getProjectId());
                    staffContractService.insertCldStaffContract(staffContract);
                }
                // 解绑之前人员
                publicCommonService.staffOptions(cldProjectPost.getStaffId(), 3, cldProjectPost.getProjectId(), cldProjectPost.getProjectPostId());
            }
        }
    }

    /**
     * 修改项目岗位
     *
     * @param cldProjectPost 项目岗位
     * @return 结果
     */
    @Override
    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public int updateCldProjectPost(CldProjectPost cldProjectPost) {
        // 查询项目岗位字典
        Map<String, String> projectPostMap = DictUtils.getDictDataMap("cld_project_post");
        CldProjectPost post = cldProjectPostMapper.selectCldProjectPostByProjectPostId(cldProjectPost.getProjectPostId());
        updateStaffContract(cldProjectPost, post);
        // 修改岗位名称及仓库名称
        String staffName = "暂无人员";
        if (StringUtils.isEmpty(cldProjectPost.getStaffName())) {
            if (cldProjectPost.getStaffId() != null) {
                CldStaff cldStaff = cldStaffService.selectCldStaffByStaffId(cldProjectPost.getStaffId());
                staffName = cldStaff.getStaffName();
            }
        } else {
            staffName = cldProjectPost.getStaffName();
        }
        if (cldProjectPost.getProjectPostName() == null && post.getProjectPostName() == null) {
            throw new RuntimeException("请配置岗位后再进行分配人员操作！");
        }
        cldProjectPost.setProjectPostNo(projectPostMap.get(cldProjectPost.getProjectPostName() == null ? post.getProjectPostName().toString() : cldProjectPost.getProjectPostName().toString()) + "-" + staffName);
        // 修改仓库名称
        ErpWareHouse erpWareHouse = new ErpWareHouse();
        erpWareHouse.setStaffId(post.getProjectPostId());
        erpWareHouse.setWareName(cldProjectPost.getProjectPostNo());
        remoteWareService.updateWareNameByPostId(erpWareHouse, SecurityConstants.INNER);
        cldProjectPostMapper.updateCldProjectPost(cldProjectPost);
        if (cldProjectPost.getStaffId() != null) {
            cldProjectPost.setIsMasterDuits(null);
            // 修改当前人员所有岗位主职务为0
            cldProjectPostMapper.updateMasterDuitsByProjectIdAndStaffId(cldProjectPost.getProjectId(), cldProjectPost.getStaffId());
            // 查询最大岗位Id
            Long projectPostId = cldProjectPostMapper.getProjectPostIdByOrderByPostNameAsc(cldProjectPost.getProjectId(), cldProjectPost.getStaffId());
            // 设置岗位id最小的那条数据为主职务
            cldProjectPostMapper.updateSetMasterDuitsByProjectIdAndStaffId(projectPostId);
            if (cldProjectPost.getClassesId() != null) {
                // 查询班次是否与之前岗位设置一致
                int flag = cldProjectPostMapper.selectMasterDuitsClass(cldProjectPost.getStaffId(), cldProjectPost.getProjectId(), cldProjectPost.getClassesId());
                if (flag <= 0) {
                    throw new RuntimeException("一人多岗情况下班次必须一致！");
                }
            }
        }
        return 1;
    }

    @Override
    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public int unbindingCldProjectPost(CldProjectPost cldProjectPost) {
        // 修改岗位名称及仓库名称
        Map<String, String> projectPostMap = DictUtils.getDictDataMap("cld_project_post");
        if (cldProjectPost.getProjectPostName() != null) {
            cldProjectPost.setProjectPostNo(projectPostMap.get(cldProjectPost.getProjectPostName().toString()) + "-暂无人员");
            // 修改仓库名称
            ErpWareHouse erpWareHouse = new ErpWareHouse();
            erpWareHouse.setStaffId(cldProjectPost.getProjectPostId());
            erpWareHouse.setWareName(cldProjectPost.getProjectPostNo());
            remoteWareService.updateWareNameByPostId(erpWareHouse, SecurityConstants.INNER);
            cldProjectPostMapper.updateCldProjectPost(cldProjectPost);
        }
        publicCommonService.staffOptions(cldProjectPost.getStaffId(), 3, cldProjectPost.getProjectId(), cldProjectPost.getProjectPostId());
        return 1;
    }


    /**
     * 删除项目岗位信息
     *
     * @param projectPostId 项目岗位主键
     * @return 结果
     */
    @Override
    public int deleteCldProjectPostByProjectPostId(Long projectPostId) {
        return cldProjectPostMapper.deleteCldProjectPostByProjectPostId(projectPostId);
    }
}
